#include "participantMenu.h"
#include "cleanScan.h"
#include <limits.h>

void participantMenu(struct UserVars userData){
	struct ConfigVars *configData;
	loadConfigs("data/Configuracion.txt",&configData);
	struct PlantillaVars *plantillaData;
	loadPlantillas("data/Plantillas.txt",&plantillaData);

	int run=1;
	int op;
	printf("Welcome %s\n",userData.name);
	while(run){
        printf("*****Participant Menu*****\n");
        printf("Select an option\n");
        printf("<1>Create roster\n");
        printf("<2>Configure roster\n");
        printf("<3>List rosters\n");
        printf("<4>Delete roster\n");
        printf("<5>Show ranking\n");
        printf("<6>Exit\n");
        scanf("%d",&op);
		char ch = getchar();
		int change=0;
		switch(op){
			case 1:
				createRoster(userData,configData,&plantillaData);
				break;
			case 2:
				confRoster(userData,configData,plantillaData);
				change=1;
				break;
			case 3:
				listRosters(userData,plantillaData);
				break;

			case 4:
				deleteRoster(userData,&plantillaData);
				break;
			case 6:
				run=0;
				break;
		}
		//update roster rating
		if(change){
		//TODO
		}
	}
	writePlantillas("outputfiles/newPlantillas.txt",plantillaData);
}

void printRanking(struct PlantillaVars *plantillaData){
	int iThird=-1;
	int vThird=INT_MIN;
	int iSecond=-1;
	int vSecond=INT_MIN;
	int iFirst=-1;
	int vFirst=INT_MIN;
	for(int i=0;i<plantillacount;i++){
		if(plantillaData[i].points > vFirst){
			iThird=iSecond;
			vThird=vSecond;
			iSecond=iFirst;
			vSecond=vFirst;
			iFirst=i;
			vFirst=plantillaData[i].points;
		}
		else if(plantillaData[i].points > vSecond){
			iThird=iSecond;
			vThird=vSecond;
			iSecond=i;
			vSecond=plantillaData[i].points;
		}
		else if(plantillaData[i].points > vThird){
			iThird=i;
			vThird=plantillaData[i].points;
		}
	}
	printf("Top 3 rosters:\n");
	printf("First:%s with %d points\n",plantillaData[iFirst].name,plantillaData[iFirst].points);
	printf("Second:%s with %d points\n",plantillaData[iSecond].name,plantillaData[iSecond].points);
	printf("Third:%s with %d points\n",plantillaData[iThird].name,plantillaData[iThird].points);
}

void createRoster(struct UserVars userData,struct ConfigVars *configData,struct PlantillaVars **plantillasData_ptr){
	struct PlantillaVars *plantillasData=*plantillasData_ptr;
	char rosterName[30];
	printf("Input roster name\n");
	fgets(rosterName,sizeof(rosterName),stdin);
	cleanString(rosterName);
	printf("\n%s",rosterName);
	printf("Max teams:%d\n",configData[0].value);
	int numTeams=0;
	int curPlantID=1;
	for(int i=0;i<plantillacount;i++){
		if(userData.userID == plantillasData[i].userID){
			numTeams++;
		}
		curPlantID++;
	}
	if(numTeams < configData[0].value){
		struct PlantillaVars newPlantilla;
		strcpy(newPlantilla.name,rosterName);
		newPlantilla.userID=userData.userID;
		newPlantilla.budget=configData[1].value;
		newPlantilla.points=0;
		//sprintf(newPlantilla.plantillaID,"00%i",curPlantID);
		newPlantilla.plantillaID=curPlantID;
		addPlantilla(newPlantilla,&plantillasData);
		*plantillasData_ptr=plantillasData;
	}
	else{
		printf("You have made too many rosters already\n");
	}
}

void listPlayersOnRoster(int rosterID,struct JugadoresPlantillaVars *roster_players_data, struct FutbolistaVars *players){
	for(int i=0;i<jugadoresPlantillacount;i++){
		if(roster_players_data[i].plantillaID == rosterID){
			for(int j=0;j<futbolistacount;j++){
				if(players[j].playerID == roster_players_data[i].playerID){
					printf("Player ID:%d Name:%s\n",players[j].playerID,players[j].name);
				}
			}
		}
	}
}


int playerIsInRoster(int rosterID, struct JugadoresPlantillaVars *rosters_players_data,struct FutbolistaVars player){
	int inRoster=0;
	for(int i=0;i<jugadoresPlantillacount;i++){
		if(rosters_players_data[i].playerID == player.playerID && rosters_players_data[i].plantillaID == rosterID){
			inRoster=1;
			break;
		}
	}
	return inRoster;
}

void listAvailablePlayers(int rosterID, struct JugadoresPlantillaVars *rosters_players_data,struct FutbolistaVars *players){
	printf("Available players:");
	for(int i=0;i<futbolistacount;i++){
		if(!playerIsInRoster(rosterID,rosters_players_data,players[i])){
			printf("Player ID:%d\nName:%s\nPrice:%d\n\n",players[i].playerID,players[i].name,players[i].price);
		}
	}
}

void addPlayerToRoster(int rosterID,struct JugadoresPlantillaVars **rosters_players_data_ptr,struct FutbolistaVars *players,struct PlantillaVars *rosters){
	struct JugadoresPlantillaVars *rosters_players_data=*rosters_players_data_ptr;
	printf("Input ID of player to add\n");
	int playerID;
	scanf("%d",&playerID);
	char ch = getchar();
	int rIndex=-1,pIndex=-1;
	for(int i=0;i<plantillacount;i++){
		if(rosters[i].plantillaID == rosterID){
			rIndex=i;
		}
	}
	for(int i=0;i<futbolistacount;i++){
		if(players[i].playerID==playerID){
			pIndex=i;
		}
	}
	if(rIndex == -1 || pIndex == -1 || playerIsInRoster(rosterID,rosters_players_data,players[pIndex])){
		printf("Player/Roster not found or player is already in roster\n");
	}
	else{
		if(players[pIndex].price < rosters[rIndex].budget){
			struct JugadoresPlantillaVars newRPD;
			newRPD.playerID=playerID;
			newRPD.plantillaID=rosterID;
			addJugadoresPlantilla(newRPD,&rosters_players_data);
			rosters[rIndex].budget-=players[pIndex].price;
			*rosters_players_data_ptr=rosters_players_data;
			printf("Player added!\n");
		}
		else{
			printf("Not enough money. Player costs %d, your available funds for this roster are %d",players[pIndex].price,rosters[rIndex].budget);
		}
	}
}

void deleteRoster(struct UserVars userData,struct PlantillaVars **rosters_ptr){
	struct PlantillaVars *rosters=*rosters_ptr;
	int rosterID;
	printf("Input ID of roster to delete:\n");
	scanf("%d",&rosterID);
	char ch=getchar();
	int index=-1;
	for(int i=0;i<plantillacount;i++){
		if(rosters[i].plantillaID == rosterID){
			index=i;
		}
	}
	if(rosters[index].userID == userData.userID){
		rmPlantilla(index,&rosters);
		*rosters_ptr=rosters;
	}
	else{
		printf("Error, roster of this ID does not belong to you!\n");
	}
}

void deleteFromRoster(int rosterID,struct JugadoresPlantillaVars **rosters_players_ptr){
	printf("Input ID of player to remove\n");
	int playerID;
	scanf("%d",&playerID);
	char ch = getchar();
	int index=-1;
	struct JugadoresPlantillaVars *rosters_players=*rosters_players_ptr;
	for(int i=0;i<jugadoresPlantillacount;i++){
		if(rosters_players[i].playerID == playerID && rosters_players[i].plantillaID == rosterID){
			index=i;
		}
	}
	if(index != -1){
		rmJugadoresPlantilla(index,&rosters_players);
		*rosters_players_ptr=rosters_players;
	}
	else{
		printf("Player not found in this roster\n");
	}
}

void confRosterMenu(struct UserVars userData, int maxPlayers, int plantillaID,struct PlantillaVars *rosters){
	struct JugadoresPlantillaVars *rosters_players_data;
	loadJugadoresPlantillas("data/Jugadores_Plantillas.txt",&rosters_players_data);
	struct FutbolistaVars *players;
	loadFutbolistas("data/Futbolistas.txt",&players);
	system("clear");
	int run=1;
	int op;
	int change=0;
	while(run){
        printf("*****Participant Menu*****\n");
        printf("Select an option\n");
        printf("<1>List players on roster\n");
        printf("<2>List available players\n");
        printf("<3>Add player to roster\n");
        printf("<4>Delete player from roster\n");
        printf("<5>Back\n");
        scanf("%d",&op);
		char ch = getchar();
		switch(op){
			case 1:
				listPlayersOnRoster(plantillaID,rosters_players_data,players);
				break;
			case 2:
				listAvailablePlayers(plantillaID,rosters_players_data,players);
				break;
			case 3:
				addPlayerToRoster(plantillaID,&rosters_players_data,players,rosters);
				change=1;
				break;
			case 4:
				deleteFromRoster(plantillaID,&rosters_players_data);
				change=1;
				break;
			case 5:
				run=0;
				break;
		}
	}
	free(players);
	writeJugadoresPlantillas("outputfiles/Jugadores_Plantillas.txt",rosters_players_data);
}

void confRoster(struct UserVars userData,struct ConfigVars *configData,struct PlantillaVars *plantillasData){
	for(int i=0;i<plantillacount;i++){
		if(plantillasData[i].userID == userData.userID){
			printf("Index:%d\tRoster name:%s\n",i,plantillasData[i].name);
		}
	}
	int index;
	printf("Input roster index\n");
	scanf("%d",&index);
	char ch = getchar();
	if(plantillasData[index].userID == userData.userID){
		printf("Going to cofig menu\n");
		confRosterMenu(userData,configData[2].value,plantillasData[index].plantillaID,plantillasData);
	}
	else{
		printf("That roster doesn't belong to you\n");
	}
}

void listRosters(struct UserVars userData,struct PlantillaVars *rosters){
	int totalPoints=0;
	for(int i=0;i<plantillacount;i++){
		if(rosters[i].userID == userData.userID){
			printf("Roster ID:%03d\nRoster name:%s\nRemaining funds:%d\nRating:%d\n\n",
					rosters[i].plantillaID,
					rosters[i].name,
					rosters[i].budget,
					rosters[i].points);
			totalPoints+=rosters[i].points;
		}
	}
	printf("Total points of your teams:%d\n",totalPoints);
}
